package com.tallysystem.servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.tallysystem.dao.UserDao;
import com.tallysystem.daoimpl.UserDaoImpl;
import com.tallysystem.entity.User;

@WebServlet(urlPatterns = { "/doupdateUser" })
public class DoUpdateUserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取表单提交信息
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		int number = Integer.parseInt(request.getParameter("number"));
		int id = Integer.parseInt(request.getParameter("id"));
		String stuclass = request.getParameter("stuclass");
		String sex = request.getParameter("sex");
		//string 转成 date
		String birth01 = request.getParameter("birth");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date();
		try {
			date = sdf.parse(birth01);
        } catch (Exception e) {
            e.printStackTrace();

        }
		java.sql.Date birth = new java.sql.Date(date.getTime());
		
		UserDao userDao = new UserDaoImpl();
		User user = new User(username,password,number,id,stuclass,sex,birth);
 
		int res = userDao.updateByname(user);	
		if (res >= 0) {
				response.sendRedirect("userview");
				System.out.println("修改成功");
		} else {
			response.sendRedirect("error.jsp");
			System.out.println("修改失败");
		}

		

	}


	protected void doGost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

}
